from time import sleep

import page
from base.base import Base
from tool.get_log import GetLogger

log = GetLogger.get_log()


class PageDiscovery(Base):

    def pagediscovery_click_discovery_tab(self):
        self.base_click_element(page.discovery_tab)

    def pagediscovery_click_card_content_1(self):
        self.base_click_element(page.discovery_card_content_1)

    def pagediscovery_click_share(self):
        self.base_click_element(page.discovery_card_content_1_share_btn)

    def pagediscovery_click_share_to_qq(self):
        self.base_click_element(page.discovery_card_content_1_share_qq)

    def pagediscovery_click_share_permission(self):
        try:
            self.base_click_element(page.right_next_step)
        except:
            pass
        self.base_click_element(page.discovery_card_content_1_share_permission)

    def pagediscovery_click_dianzan(self):
        self.base_click_element(page.discovery_card_content_1_dianzan)

    def pagediscovery_get_dianzan_number(self):
        str_dianzan = self.base_get_element_text(page.discovery_card_content_1_dianzan)
        return self.base_get_string_number(str_dianzan)

    def pagediscovery_return_to_discoverypage_keycode(self):
        for a in range(10):
            log.info(f'正在返回设置页面，第{a + 1}次尝试')
            if self.base_if_element_exist(page.discovery_tab):
                break
            else:
                self.driver.press_keycode(4)

    def pagediscovery_if_share_success(self):
        if self.base_get_current_package_name() == 'com.tencent.mobileqq':
            return True
        else:
            return False

    def pagediscovery_swipe_find_dianzan(self):
        return self.base_up_to_down_find_element(
            page.discovery_card_swipe_area, page.discovery_card_content_1_dianzan)

    """
    以上是作业
    """

    def pagediscovery_click_banner(self):
        self.base_click_element(page.fx_banner)

    def pagediscovery_if_banner_success(self):
        return self.base_if_element_exist(page.discovery_banner_assert)

    def pagediscovery_click_ai_study(self):
        self.base_click_element(page.fx_AI_study)

    def pagediscovery_click_ai_adj(self):
        self.base_click_element(page.discovery_ai_adj)

    def pagediscovery_if_adj_success(self):
        return self.base_left_to_right_find_element\
            (page.discovery_ai_swipe_area,page.discovery_ai_adj_assert)

    def pagediscovery_click_ai_addr(self):
        self.base_click_element(page.discovery_ai_addr)

    def pagediscovery_if_addr_success(self):
        return self.base_left_to_right_find_element \
            (page.discovery_ai_swipe_area, page.discovery_ai_addr_assert)

    def pagediscovery_click_ad_1(self):
        self.base_click_element(page.discovery_ad_1)

    def pagediscovery_if_ad_1_success(self):
        return self.base_if_element_exist(page.discovery_ad_1_assert)

    def pagediscovery_go_down(self):
        self.base_go_down_directly(page.discovery_ad_swipe_area)

    def pagediscovery_click_ad_2(self):
        # self.base_up_to_down_find_element(page.discovery_ad_swipe_area,page.discovery_ad_2)
        self.base_click_element(page.discovery_ad_2)

    def pagediscovery_if_ad_2_success(self):
        return self.base_if_element_exist(page.discovery_ad_2_assert)

    def pagediscovery_click_ad_3(self):
        # self.base_up_to_down_find_element(page.discovery_ad_swipe_area, page.discovery_ad_3)
        self.base_click_element(page.discovery_ad_3)

    def pagediscovery_if_ad_3_success(self):
        return self.base_if_element_exist(page.discovery_ad_3_assert)

    def pagediscovery_click_audio(self):
        self.base_click_element(page.discovery_audio)

    def pagediscovery_if_audio_success(self):
        time_1 = self.base_get_element_text(page.discovery_audio_current_time)
        self.base_click_element(page.discovery_audio_play)
        sleep(10)
        self.base_click_element(page.discovery_audio_play)
        time_2 = self.base_get_element_text(page.discovery_audio_current_time)
        if time_1 == time_2:
            return False
        else:
            return True


